<template>
    <div class="login">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <h2>系统登录</h2>
            <el-form-item prop="account">
                <el-input v-model="form.account" prefix-icon="el-icon-user"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="form.password" prefix-icon="el-icon-lock" show-password></el-input>
            </el-form-item>
            <el-button type="primary" @click="onSubmit">登录</el-button>
        </el-form>
    </div>
</template>

<script>
//axios封装第三层:使用封装好的函数
import { chenkLogin } from "@/api/user.js" //普通引入需要解构
import loacl from '@/utils/local.js'

export default {
    data () {
        return {
            form: {
                account: '',
                password: ''
            },
            // 验证规则
            rules: {
                account: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, message: '长度在 3 个字符以上', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 6, message: '长度在 6 个字符以上', trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        onSubmit () {
            this.$refs.form.validate(async result => {
                if (result) {
                    //验证通过,提交数据去登录
                    let res = await chenkLogin(this.form)
                    if (res.data.code === 0) {
                        //存储token
                        loacl.set('token', res.data.token)
                        loacl.set('role', res.data.role)
                        //跳转到首页
                        this.$router.push('/index')
                    }
                }

            })
            // this.$router.push('./index')
        }
    }

}

</script>

<style lang="less">
.login {
    height: 100%;
    background: url(../../assets/images/loginbg.jpg) no-repeat center/cover;
    display: flex;
    justify-content: center;
    align-items: center;

    .el-form {
        width: 350px;
        color: #fff;
        text-align: center;
        padding: 20px;
        border-radius: 20px;
        background-color: rgba(0, 0, 0, 0.7);

        .el-form-item__content {
            margin: 0 !important;
        }

        .el-input {
            margin: 20px 0;

            input {
                background-color: rgba(0, 0, 0, 0.3);
                color: #fff
            }
        }

        .el-button {
            width: 100%;
        }
    }

}
</style>